package com.snow.component;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.snow.exception.CommonRuntimeException;
import com.snow.common.entity.SystemConfigEntity;
import com.snow.common.entity.SystemConfigItemEntity;
import com.snow.common.repository.SystemConfigItemRepository;
import com.snow.common.repository.SystemConfigRepository;
import com.snow.service.IAuthComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Slf4j
@Component(value = "SnowSystemConfigComponent")
public class SystemConfigComponent {

    private final SystemConfigRepository systemConfigRepository;
    private final SystemConfigItemRepository systemConfigItemRepository;
    private final IAuthComponent authComponent;

    public SystemConfigComponent(SystemConfigRepository systemConfigRepository, SystemConfigItemRepository systemConfigItemRepository, IAuthComponent authComponent) {
        this.systemConfigRepository = systemConfigRepository;
        this.systemConfigItemRepository = systemConfigItemRepository;
        this.authComponent = authComponent;
    }

    public Optional<SystemConfigItemEntity> getConfig(Integer configId) {
        return getConfig(configId, null);
    }

    public Optional<SystemConfigItemEntity> getConfig(String configCode) {
        return getConfig(configCode, null);
    }

    public Optional<SystemConfigItemEntity> getConfig(Integer configId, String configObjId) {
        List<SystemConfigItemEntity> list = getConfigs(configId, configObjId);
        if (list.isEmpty()) {
            return Optional.empty();
        } else if (list.size() == 1) {
            return Optional.of(list.get(0));
        } else {
            throw new CommonRuntimeException("参数配置信息信息不唯一，请联系管理员处理");
        }
    }

    public Optional<SystemConfigItemEntity> getConfig(String configCode, String configObjId) {
        List<SystemConfigItemEntity> list = getConfigs(configCode, configObjId);
        if (list.isEmpty()) {
            return Optional.empty();
        } else if (list.size() == 1) {
            return Optional.of(list.get(0));
        } else {
            throw new CommonRuntimeException("参数配置信息信息不唯一，请联系管理员处理");
        }
    }

    public List<SystemConfigItemEntity> getConfigs(Integer configId, String configObjId) {
        Optional<SystemConfigEntity> optionalConfig = systemConfigRepository.findById(configId);
        if (!optionalConfig.isPresent()) {
            throw new CommonRuntimeException("配置信息不存在，请联系管理员");
        }
        SystemConfigEntity config = optionalConfig.get();
        if (StrUtil.isBlank(configObjId)) {
            if (config.getType() == 1) {
                configObjId = authComponent.getCurrentUser().getId().toString();
            } else if (config.getType() == 2) {
                // 本地配置项，不走数据库查询,返回空列表
                return CollUtil.newArrayList();
            } else if (config.getType() == 3) {
                configObjId = authComponent.getCurrentUser().getTenantId().toString();
            } else if (config.getType() == 5) {
                configObjId = authComponent.getCurrentUser().getDeptId().toString();
            } else {
                log.error("暂不支持的字典类型，请联系管理员：{}", config.getType());
                throw new CommonRuntimeException("暂不支持的字典类型，请联系管理员");
            }
        }
        return systemConfigItemRepository.lambdaQuery()
                .equal("configId", configId)
                .equal("configObjId", configObjId)
                .findAll();
    }

    public List<SystemConfigItemEntity> getConfigs(String configCode, String configObjId) {
        SystemConfigEntity config = systemConfigRepository.lambdaQuery().equal("code", configCode).one();
        // 域名直接加载
        if (config.getType() == 4) {
            return systemConfigItemRepository.lambdaQuery()
                    .equal("configId", config.getId())
                    .equal("configObjId", configObjId)
                    .findAll();
        }
        return getConfigs(config.getId(), configObjId);
    }

}
